package com.yitao.cms.controller.user.cmsUser;


import com.skipper.base.bean.*;
import com.yitao.cms.config.aopLog.LogAnnotation;
import com.yitao.cms.dto.operationUser.OperationUserCmsDto;
import com.yitao.cms.service.user.cmsUser.OperationUserRoleService;
import com.yitao.cms.service.user.cmsUser.OperationUserService;
import com.yitao.cms.utils.StringDealUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@Api(value = "user", description = "用户模块")
@RequestMapping("/user")
@CrossOrigin
@RestController
public class OperationUserController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private OperationUserService userService;

    @Autowired
    private OperationUserRoleService userRoleService;

    @ApiOperation("用户添加/编辑")
    @LogAnnotation(targetType = "user-updateUser", remark = "用户添加/编辑")
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public DataOutput updateUser(@RequestBody OperationUserCmsDto userDto, HttpServletRequest request) {
        return userService.updateUser(userDto, request);
    }

    @ApiOperation("用户登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public DataOutput userLogin(String phone, String password, HttpServletRequest request) {
        logger.info("==用户登录:userLogin==" + "入参:" + "phone" + phone + "password:" + password);
        Assert.hasText(phone, "用户名不能为空");
        Assert.hasText(password, "密码不能为空");
        return userService.userLogin(StringDealUtils.dealBlankString(phone),StringDealUtils.dealBlankString(password), request);
    }

    @ApiOperation("用户注册")
    @LogAnnotation(targetType = "user-register", remark = "用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public DataOutput register(String phone, String password, HttpServletRequest request) {

        return userService.register(StringDealUtils.dealBlankString(phone),StringDealUtils.dealBlankString(password), request);
    }


    @ApiOperation("获取验证码")
    @RequestMapping(value = "/getVerifyCode", method = RequestMethod.POST)
    public DataOutput getVerifyCode(@ApiParam(value = "手机号 ", required = true) @RequestParam(value = "phone", required = true) String phone,
                                    HttpServletRequest request) {
        return userService.getVerifyCode(phone, request);
    }

    @ApiOperation("校验验证码是否正确")
    @RequestMapping(value = "/checkoutVerifyCode", method = RequestMethod.POST)
    public DataOutput checkoutVerifyCode(@ApiParam(value = "手机号 ", required = true)
                                         @RequestParam(value = "phone", required = true) String phone,
                                         @ApiParam(value = "验证码 ", required = true)
                                         @RequestParam(value = "verifyCode", required = true) String verifyCode, HttpServletRequest request) {
        return userService.checkoutVerifyCode(phone, verifyCode, request);
    }

    @ApiOperation("忘记密码")
    @LogAnnotation(targetType = "user-updatePassword", remark = "忘记密码")
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public DataOutput updatePassword(@ApiParam(value = "手机号 ", required = true) @RequestParam(value = "phone", required = true) String phone,
                                     @ApiParam(value = "新密码 ", required = true) @RequestParam(value = "password", required = true) String password,
                                     HttpServletRequest request) {
        return userService.updatePassword(StringDealUtils.dealBlankString(phone),StringDealUtils.dealBlankString(password), request);
    }

    @ApiOperation("查询用户列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public PagedOutput<OperationUserCmsDto> findUserList(
            @ApiParam(value = "用户名") @RequestParam(value = "userName", required = false) String userName,
            @ApiParam(value = "手机号码") @RequestParam(value = "phone", required = false) String phone,
            @ApiParam(value = "角色Id") @RequestParam(value = "roleId", required = false) Long roleId,
            @ApiParam(value = "分页参数，表示当前页", required = true, defaultValue = "1") @RequestParam(value = "pageNo") Integer pageNo,
            @ApiParam(value = "分页参数，表示每页显示多少条记录", required = true, defaultValue = "10") @RequestParam(value = "pageSize") Integer pageSize) {

        return userService.userList(userName, phone, roleId, pageNo, pageSize);
    }

    @ApiOperation("用户禁用/启用")
    @LogAnnotation(targetType = "user-updateStateById", remark = "用户禁用/启用")
    @RequestMapping(value = "/updateStateById", method = RequestMethod.POST)
    public DataOutput updateStateById(@ApiParam(value = "用户Id ", required = true)
                                      @RequestParam(value = "userId", required = true) Long userId,
                                      @ApiParam(value = "是否可用；true:可用，false:不可用 ", required = true)
                                      @RequestParam(value = "state") Boolean state, HttpServletRequest request) {
        return userService.updateStateById(userId, state, request);
    }


    @ApiOperation("用户退出")
    @RequestMapping(value = "/login/out", method = RequestMethod.POST)
    public DataOutput loginOut(HttpServletRequest request, HttpServletResponse response) {
        return userService.loginOut(request);
    }

    @ApiOperation("用户详情")
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public DataOutput<OperationUserCmsDto> getUserDetail(@ApiParam(value = "用户Id ", required = true) @RequestParam(value = "userId") Long userId
    ) {
        return userService.getDetails(userId);
    }

}

